package gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.*;

import netGaming.NetPlayer;

import player.AIPlayer;
import player.MovementSource;
import player.Player;

/**
 * @author Wojciech Łopata
 *
 */

public class PlayerSelectionPanel extends JPanel
{
	private static final long serialVersionUID = 7047242125848738774L;
	
	private final JComboBox playerTypeComboBox;
	private final PortField portField;
	
	public PlayerSelectionPanel()
	{
		super();
		setLayout(new FlowLayout());
		
		playerTypeComboBox = new JComboBox(PlayerType.values());
		playerTypeComboBox.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				portField.setEditable(false);
				portField.setText("");
				if(playerTypeComboBox.getSelectedItem().equals(PlayerType.NET))
					portField.setEditable(true);
				
			}
		});
		add(playerTypeComboBox);
		
		add(new JLabel("port: "));
		portField = new PortField();
		portField.setEditable(playerTypeComboBox.getSelectedItem().equals(PlayerType.NET));
		add(portField);
	}
	
	public Player getPlayer(MovementSource localPlayerMoveSource) throws IOException
	{
		switch((PlayerType) playerTypeComboBox.getSelectedItem())
		{
			case NET:
				return new NetPlayer(portField.getPort());
			case LOCAL:
				return new GUIPlayer(localPlayerMoveSource);
			case AI:
				return new AIPlayer();
		}
		return null; 
	}
	
	private enum PlayerType
	{
		LOCAL
		{
			@Override
			public String toString()
			{
				return "lokalny";
			}
		},
		
		NET
		{
			@Override
			public String toString()
			{
				return "zdalny";
			}
		},
		
		AI
		{
			@Override
			public String toString()
			{
				return "komputer";
			}
		};
	}
}
